from afast_core.core_tools.encrypt_helper import md5
from afast_core.core_tools.jwt_helper import SimpleAuth
from infirmary_admin_src.infirmary_common.infirmary_controller import IBaseController
from infirmary_admin_src.infirmary_common.infirmary_controller.rest_controller_with_depends import \
    RestControllerWithDepends
from fastapi import Request

from infirmary_admin_src.infirmary_smart_admin.infirmary_biz.system.login.schemas import SchemaBase
from infirmary_admin_src.infirmary_smart_admin.infirmary_biz.system.login.services import LogicBusinesses
from infirmary_admin_src.infirmary_smart_admin.infirmary_utils.convert2_help import convert_dict_to_camel_case
from fastapi import Depends

class XParam(SchemaBase):
    '''
    账号密码登录参数
    '''
    pass


class IController(IBaseController):
    '''
    定制相关约束检测函数逻辑步序
    '''

    def __init__(self, *, request: Request,
                 schema_param: XParam=Depends(),
                 ):
        super().__init__(request=request)
        # 入参参数
        # 入参参数
        self.schema_param = schema_param


@RestControllerWithDepends()
class GetDupdateAuthloginController(IController):

    def business_login(self):
        pass

        # 开始读取用户信息
        result_user_info = LogicBusinesses.get_sys_employee_info_by_login_name(login_name=self.request.state.login_name)
        # 开始生成用户token以及其他信息
        _date = {
            'login_name': self.request.state.login_name,
            'employee_id': self.request.state.employee_id,
            'administrator_flag': self.request.state.administrator_flag,
        }
        # 生成对于的此用户信息的token, 创建授权码，并设置过期的时间--授权码有效期为 24 * 31天
        isok, token = SimpleAuth.create_token_by_data(sub='zyx', data=_date, exp_time=60 * 60 * 2)
        if result_user_info.administrator_flag == 1:
            menuList = LogicBusinesses.get_sys_employee_menuList_by_admin_role()
            authorities = LogicBusinesses.get_sys_employee_authorities_by_admin_role()
        else:
            menuList = LogicBusinesses.get_sys_employee_menuList_by_admin_role_byEmployeeid(employee_id=result_user_info.employee_id)
            authorities = LogicBusinesses.get_sys_employee_authorities_by_admin_role()
        backdata = {
            # ============
            "token": token,
            'userId': result_user_info.id,
            'employeeId': result_user_info.id,
            'userType': "ADMIN_EMPLOYEE" if result_user_info.administrator_flag == 1 else '',
            'username': result_user_info.login_name,
            'userName': result_user_info.actual_name,
            'loginName': result_user_info.login_name,
            'actualName': result_user_info.actual_name,
            'gender': result_user_info.gender,
            'phone': result_user_info.phone,
            'departmentId': result_user_info.department_id,
            'departmentName': True if result_user_info.administrator_flag == 1 else False,
            'enabled': True if result_user_info.disabled_flag == 0 else False,
            "accountNonExpired": True,
            "accountNonLocked": True,
            "credentialsNonExpired": True,
            # ============
            'ip': None,
            'lastLoginIp': '',
            'lastLoginTime': None,
            'lastLoginUserAgent': None,
            # 用户角色享有的菜单列表
            "menuList": [{**{'apiPermsList': None}, **convert_dict_to_camel_case(v, passkey=['contextMenuId',
                                                                                             'createUserId',
                                                                                             'menuType',
                                                                                             'parentId',
                                                                                             ],
                                                                                 keepkey=['permsType', 'updateUserId',
                                                                                          'menuId', ],

                                                                                 ),
                          } for v in menuList] if menuList else [],
            # 用户角色有的权限标识
            "authorities": authorities,
        }
        return backdata
